/*
 * Created on 2006-10-25
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.CompoundBorder;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.swt.graphics.Color;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.model.core.CorePackage;
import edu.pku.sei.modeler.model.structure.MetaLocationManager;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;

/**
 * @deprecated
 * @author zhangle
 *
 */
public class TemplateSubNodeEditPart extends AbstractNodeEditPart
		implements PropertyChangeListener, NodeEditPart {

	public TemplateSubNodeEditPart(MetaShapeElement model) {
		super(model);
	}

	private ConnectionAnchor anchor;

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		Label label = new Label("TemplateSubNode");
		CompoundBorder b = new CompoundBorder(new LineBorder(ColorConstants.buttonDarker,1), new MarginBorder(1));
		label.setBorder(b);
		label.setBackgroundColor(new Color(null,200,200,255));
		label.setOpaque(true);
		return label;
	}
	
	public void activate() {
		if (isActive())
			return;
		super.activate();
		getNode().addPropertyChangeListener(this);
		getNode().getModelElement().addPropertyChangeListener(this);
	}

	public void deactivate() {
		if (!isActive())
			return;
		super.deactivate();
		getNode().removePropertyChangeListener(this);
		getNode().getModelElement().removePropertyChangeListener(this);
	}
	
	public MetaNode getNode() {
		return (MetaNode) getModel();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		
	}

	/* (non-Javadoc)
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getPropertyName().equals(StructurePackage.NODE_HEIGHT)) {
			refreshVisuals();
		}
		else if(evt.getPropertyName().equals(StructurePackage.NODE_WIDTH)) {
			refreshVisuals();
		}
		else if(evt.getPropertyName().equals(StructurePackage.NODE_LOCATIONX)) {
			refreshVisuals();
		}
		else if(evt.getPropertyName().equals(StructurePackage.NODE_LOCATIONY)) {
			refreshVisuals();
		}
		else if(evt.getPropertyName().equals(StructurePackage.SHAPEELEMENT_ELEMENT)) {
			refreshVisuals();
		}
		else if(evt.getPropertyName().equals(CorePackage.CLASS_ATTRIBUTES)) {
			refreshVisuals();
		}

	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.Request)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	protected ConnectionAnchor getConnectionAnchor() {
		if (anchor == null)
			anchor = new ChopboxAnchor(getFigure());

		return anchor;
	}
	
	protected void refreshVisuals() {
//		MetaNode node = getNode();
//		int x = node.getLocationX();
//		int y = node.getLocationY();
//		int width = node.getWidth();
//		int height = node.getHeight();
//		Rectangle r = new Rectangle(x, y, width, height);
//		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
//				getFigure(), r);
//		List mgrs = node.getLocationMgrs();
//		for(int i=0; i<mgrs.size(); i++) {
//			MetaLocationManager mgr = (MetaLocationManager)mgrs.get(i);
//			mgr.locateSubParts(r);
//		}
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		// TODO Auto-generated method stub
		return null;
	}
}
